--
-- Author: ping.wang
-- Date: 2018-08-08 18:57:17
--
-- RewardWelfarePanel
--
local ListMenu = require("app.widget.ListMenu")
local RewardFirstTargetRender = import(".renders.RewardFirstTargetRender")
local RewardFirstPayPanel = import(".RewardFirstPayPanel")
local RewardMonthSignPanel = import(".RewardMonthSignPanel")
local RewardWelfareGivePanel = import(".RewardWelfareGivePanel")
local RewardFundPanel = import(".RewardFundPanel")
local RewardCDKeyPanel = import(".RewardCDKeyPanel")
local RewardWelfareThroughPanel = import(".RewardWelfareThroughPanel")
local RewardWelfareHeroTreasurePanel = import(".RewardWelfareHeroTreasurePanel")
local RewardFirstKillPanel = import(".RewardFirstKillPanel")
local RewardMonthlyPrivilege = import(".RewardMonthlyPrivilege")
local RewardAmazingBoxPanel = import(".RewardAmazingBoxPanel")

local ClassRef = app.mvc.createPanelClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardWelfarePanel))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )

    self._givePanel = RewardWelfareGivePanel.create(self._ui["Panel/give"]) --普通
    self._signPanel = RewardMonthSignPanel.create(self._ui["Panel/sign"]) --签到
    self._firstPayPanel = RewardFirstPayPanel.create(self._ui["Panel/recharge"]) --首充
    self._cdkeyPanel = RewardCDKeyPanel.create(self._ui["Panel/dhm"]) -- 兑换码
    self._throughPanel = RewardWelfareThroughPanel.create(self._ui["Panel/through"]) --快速升级礼包
    self._firstKillPanel = RewardFirstKillPanel.create(self._ui["Panel/kill"]) -- 首杀大礼
    self._fundPanel = RewardFundPanel.create(self._ui["Panel/fund"]) -- 抽奖基金
    self._privilegePanel = RewardMonthlyPrivilege.create(self._ui["Panel/monthly"])  --特权礼包（月卡）
    self._amazingBoxPanel = RewardAmazingBoxPanel.create(self._ui["Panel/noble"]) --神奇宝箱/日怒宝藏（两个活动共用一个UI）
    self._heroTreasurePanel = RewardWelfareHeroTreasurePanel.create(self._ui["Panel/mine"]) --神奇宝箱/日怒宝藏（两个活动共用一个UI）

    self._firstPayPanel:setVisible(false)
    self._givePanel:setVisible(false)
    self._signPanel:setVisible(false)
    self._cdkeyPanel:setVisible(false)
    self._throughPanel:setVisible(false)
    self._firstKillPanel:setVisible(false)
    self._fundPanel:setVisible(false)
    self._privilegePanel:setVisible(false)
    self._amazingBoxPanel:setVisible(false)
    self._heroTreasurePanel:setVisible(false)

    local function operate(op, data)
        --if data.id ~= self._selectedIdx then
            self._selectedIdx = data.id
            if op == "btnClick" then
                executeFunc(self._onOperate, "selectWelfare", data)
            elseif op == "subBtnClick" then
            end
        --end
    end

    local params = {
        list = self._ui["Panel/ListView"],
        btnClass = RewardFirstTargetRender,
        subBtnClass = RewardFirstTargetRender,
        operate = operate,
        mustHaveTarget = true,
        onlyOpen = true,
        autoHideSecond = true,
    }
    self._activityList = ListMenu.new(params)

    self:refresh()
end

function ClassRef:initLeftList(listData,curActivityId)
    if self._activityList then
        self._activityList:removeAllMenus()
        local testData = clone(listData)
        local curSelectedId = 1
        for i = 1, #testData do
            testData[i].id = i
            testData[i].subDatas = {}
            if testData[i].uuid == curActivityId then
                curSelectedId = i
            end
        end

        self._listData = listData
        self._activityList:refreshList(testData)
        self._activityList:selectBtn(curSelectedId,nil,false)
    end
end

--通知去刷新左侧红点
function ClassRef:refreshLeftListRedPoint(allActivityRedPoint)
    for k,v in ipairs(self._listData) do
        self._activityList:setBtnRedPoint(k,nil,allActivityRedPoint[v.uuid])
    end
end

-- 刷新神奇宝箱界面
function ClassRef:refreshAmazingAwardPanel(param, type)
    if self._amazingBoxPanel then
        self._amazingBoxPanel:refreshAmazingAwardPanel(param, type)
    end
end

function ClassRef:updateAmazingAwardMoney()
    if self._amazingBoxPanel then
        self._amazingBoxPanel:updateMoney()
    end
end

function ClassRef:updateThoughLvPanel()
    if self._throughPanel then
        self._throughPanel:refreshViewAfterGetCurrency()
    end
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )
        self._firstPayPanel:setVisible(false)
        self._throughPanel:setVisible(false)
        self._givePanel:setVisible(false)
        self._signPanel:setVisible(false)
        self._cdkeyPanel:setVisible(false)
        self._firstKillPanel:setVisible(false)
        self._fundPanel:setVisible(false)
        self._privilegePanel:setVisible(false)
        self._amazingBoxPanel:setVisible(false)
        self._heroTreasurePanel:setVisible(false)
        local data = self._data
        if self._selectedIdx then
            local subType = self._listData[self._selectedIdx].subType
            if subType == Enums.ActivitySubType.FirstRecharge then
                self._firstPayPanel:onOperate(self._onOperate)
                self._firstPayPanel:setData(data)
            elseif subType == Enums.ActivitySubType.Sign then
                self._signPanel:onOperate(self._onOperate)
                self._signPanel:setData(data)
            elseif subType == Enums.ActivitySubType.Give then
                self._givePanel:onOperate(self._onOperate)
                self._givePanel:setData(data)
            elseif subType == Enums.ActivitySubType.Fund then
                self._fundPanel:onOperate(self._onOperate)
                self._fundPanel:setData(data)
            elseif subType == Enums.ActivitySubType.CDKey then
                self._cdkeyPanel:onOperate(self._onOperate)
                self._cdkeyPanel:setVisible(true)
            elseif subType == Enums.ActivitySubType.Through then
                self._throughPanel:onOperate(self._onOperate)
                self._throughPanel:setData(data)
            elseif subType == Enums.ActivitySubType.FirstKill then
                self._firstKillPanel:onOperate(self._onOperate)
                self._firstKillPanel:setVisible(true)
                self._firstKillPanel:setData(data)
            elseif subType == Enums.ActivitySubType.Privilege then
                self._privilegePanel:onOperate(self._onOperate)
                self._privilegePanel:setVisible(true)
                self._privilegePanel:setData(data)
            elseif subType == Enums.ActivitySubType.AmazingBox then
                self._amazingBoxPanel:onOperate(self._onOperate)
                self._amazingBoxPanel:setVisible(true)
                self._amazingBoxPanel:setData(data)
            elseif subType == Enums.ActivitySubType.SunfuryTreasure then
                self._amazingBoxPanel:onOperate(self._onOperate)
                self._amazingBoxPanel:setVisible(true)
                self._amazingBoxPanel:setData(data)
            elseif subType == Enums.ActivitySubType.HeroTreasure then
                self._heroTreasurePanel:onOperate(self._onOperate)
                self._heroTreasurePanel:setVisible(true)
                self._heroTreasurePanel:setData(data)
            end
        end
    else
        self:setVisible( false )
    end
end

function ClassRef:setData(data)
    self._data = data
    self:refresh()
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

return ClassRef
